package com.example.yuejian.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian.R;
import com.example.yuejian.adapter.SearchAdapter;
import com.example.yuejian.adapter.VideoAdapter;
import com.example.yuejian.bean.SearchEntity;
import com.example.yuejian.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.recy)
    RecyclerView recyclerView;

    String id;
    LinearLayoutManager layoutManager;
    private Context context;

    @BindView(R.id.title_activity)
    TitleView title_activity;
    private void initView() {
        title_activity.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(SearchActivity.class);
            }
        });


    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_search);
        super.onCreate(savedInstanceState);

        context = getBaseContext();

        initView();
        layoutManager = new LinearLayoutManager(context);

        id = getIntent().getIntExtra("id", 0) + "";

        EasyHttp.get("yuejian6/public/index.php/json/search/searchitem")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    private static final String TAG = "SearchActivity";

    Handler handler_activity = new mHandler();

    RecyclerView.Adapter adapter;

    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            List<SearchEntity> searchEntities = null;

            try{
                searchEntities = JSON.parseArray(msg.obj.toString(), SearchEntity.class);
            }catch (Exception e){
                ToastUtils.showShort("获取数据失败");
                return;
            }

            adapter = new SearchAdapter(context, searchEntities, R.layout.video_item);

            //设置布局管理器
            recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
            layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
            recyclerView.setAdapter(adapter);
//设置增加或删除条目的动画
            recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
            recyclerView.setItemAnimator(new DefaultItemAnimator());


        }
    }
}
